package com.order.model.sys;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.order.shiro.ShiroUtils;
import com.order.utils.DateUtils;
import com.order.utils.JsonUtils;

/** 
* @类名: SysRoleMenu 
* @描述: 角色菜单关系
* @作者: 林勃 
* @时间: 2014年4月14日 上午11:24:12 
*  
*/ 
@TableBind(tableName = "sys_role_menu", pkName = "id")
public class SysRoleMenu extends Model<SysRoleMenu> {

	private static final long serialVersionUID = 1L;
	public static SysRoleMenu dao = new SysRoleMenu();
	public static final String ID = "id";
	public static final String SYS_ROLE_ID = "sys_role_id";
	public static final String SYS_MENU_ID = "sys_menu_id";
	public static final String LAST_UPDATED_BY = "last_updated_by";
	public static final String LAST_UPDATE_DATE = "last_update_date";
	
	/** 
	* @Title: getRoleMenu 
	* @描述: 查询用户角色
	* @param page
	* @param pagesize
	* @param menuid
	* @return   
	* Map<String,Object>    
	* @throws 
	*/ 
	public Map<String, Object> getRoleMenuByMenuId(int page, int pageSize, int menuId) {
		String select = "select rm.id id, m.id menu_id, m.name menu_name, r.id role_id, r.name role_name, r.info role_info ";		
		String sqlExceptSelect= "  from sys_menu m, sys_role r, sys_role_menu rm" 
								 +" where m.id = rm.sys_menu_id" 
								 +"   and r.id = rm.sys_role_id" 
								 +"	  and m.id = " + menuId
								 +"   order by r.name";
		Page<SysRoleMenu> list = dao.paginate(page, pageSize, select, sqlExceptSelect);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Rows", list.getList());
		map.put("Total", list.getTotalRow());
		return map;
	}

	public Map<String, Object> getRoleMenuByModuleId(int page, int pageSize,int moduleId) {
		String select = "select rm.id id, m.id menu_id, m.name menu_name, r.id role_id, r.name role_name, r.info role_info ";		
		String sqlExceptSelect= "  from sys_menu m, sys_role r, sys_role_menu rm" 
				 +" where m.id = rm.sys_menu_id" 
				 +"   and r.id = rm.sys_role_id" 
				 +"	  and m.id in (select menu.id"
				 + " 			from sys_menu menu "
				 + "			where menu.sys_module_id = " + moduleId + ")"
				 +"   order by r.name";
		Page<SysRoleMenu> list = dao.paginate(page, pageSize, select, sqlExceptSelect);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Rows", list.getList());
		map.put("Total", list.getTotalRow());
		return map;
	}
	
	/** 
	* @Title: containRoleMenu 
	* @描述: 判断是否存在对应的角色和菜单组合
	* @param roleId
	* @param menuId
	* @return   
	* boolean    
	* @throws 
	*/ 
	private boolean containRoleMenu(String roleId, String menuId){
		System.out.println(roleId+":"+menuId);
		String sql = "select count(1) row from sys_role_menu rm "
				+ " where rm.sys_role_id = " + roleId 
				+ " 	and rm.sys_menu_id = " + menuId;
		int row = dao.findFirst(sql).getNumber("row").intValue();
		System.out.println(row);
		if(row == 0){
			return false;
		}
		return true;
	}
	
	//新增用户角色
	public String mySave(){
		String msg="";
		String roleid = getStr("SYS_ROLE_ID");
		String menuid = getStr("SYS_MENU_ID");
		System.out.println(roleid+":"+menuid);
		if(!SysRole.containRole(roleid)){
			msg = "温馨提示：角色已经不存在！";
		}
		if(!SysMenu.containMenu(menuid)){
			msg = "温馨提示：菜单已经不存在！";
		}
		if( dao.containRoleMenu(roleid,menuid)){    		 
			msg = "温馨提示：已存在对应角色和菜单组合！";
		}else{
			this.set(LAST_UPDATED_BY, ShiroUtils.getUserId());
			this.set(LAST_UPDATE_DATE, DateUtils.getNowTime());
			this.save(); 
			msg = "温馨提示：添加成功!";
		}        
		return msg;
	}
	
	public boolean myUpdate(){
		set(LAST_UPDATED_BY, ShiroUtils.getUserId());
		set(LAST_UPDATE_DATE, DateUtils.getNowTime());
		return update(); 
	}
	/** 
	 * @Title: getObj 
	 * @描述: json转对象
	 * @param json
	 * @return   
	 * SysRoleMenu    
	 * @throws 
	 */ 
	public SysRoleMenu getObj(String json){
		try {
			return this.setAttrs(JsonUtils.fastjson2Map(json));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return this;
	}
}
